/*============================================================================*/
/*       UNIVERSIDAD DE GUADALAJARA - CONTINENTAL AUTOMOTIVE SYSTEMS          */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* %name:            %
* %version:         %
* %created_by:      %
* %date_created:    %
*=============================================================================*/
/* DESCRIPTION : Timers handling                                              */
/*============================================================================*/
/* FUNCTION COMMENT : Provices APIs to configure and use Timers of PIC micro  */
/*                                                                            */
/*                                                                            */
/*============================================================================*/

#include "stdtypedef.h"
#include "p18f4550.h"
#include "hw_timers.h"


/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 
/* BYTE RAM variables */


/* WORD RAM variables */
T_UWORD ruw_TimerCounter;

/* LONG and STRUCTURE RAM variables */


/* Private functions prototypes */
/* ---------------------------- */
//PRIVATE_FCT void priv_func (void);


/* Exported functions prototypes */
/* ----------------------------- */
void timer0_initCfg(void);
T_UBYTE timer0_finished(void);
void timer0_Interrupt_Handler(void);

/* APIs */
/**************************************************************
 *  Name                 : timer0_initCfg
 *  Description          : Initialize configuration parameters for timer0
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void timer0_initCfg(void)
{
	// reset values in register T0CON is 0xFF

	T0CONbits.TMR0ON = 0;
	/*
	bit 7 TMR0ON: Timer0 On/Off Control bit
	1 = Enables Timer0
	0 = Stops Timer0
	*/
	T0CONbits.T08BIT = 0;
	/*
	bit 6 T08BIT: Timer0 8-Bit/16-Bit Control bit
	1 = Timer0 is configured as an 8-bit timer/counter
	0 = Timer0 is configured as a 16-bit timer/counter
	*/
	T0CONbits.T0CS = 0;
	/*
	bit 5 T0CS: Timer0 Clock Source Select bit
	1 = Transition on T0CKI pin
	0 = Internal instruction cycle clock (CLKO)
	*/
	/*
	bit 4 T0SE: Timer0 Source Edge Select bit
	1 = Increment on high-to-low transition on T0CKI pin
	0 = Increment on low-to-high transition on T0CKI pin
	*/
	T0CONbits.PSA = 1;
	/*
	bit 3 PSA: Timer0 Prescaler Assignment bit
	1 = TImer0 prescaler is NOT assigned. Timer0 clock input bypasses prescaler.
	0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.
	*/
	T0CONbits.T0PS2 = 0;
	T0CONbits.T0PS1 = 0;
	T0CONbits.T0PS0 = 0;
	/*
	bit 2-0 T0PS2:T0PS0: Timer0 Prescaler Select bits
	111 = 1:256 Prescale value
	110 = 1:128 Prescale value
	101 = 1:64 Prescale value
	100 = 1:32 Prescale value
	011 = 1:16 Prescale value
	010 = 1:8 Prescale value
	001 = 1:4 Prescale value
	000 = 1:2 Prescale value
	*/
	INTCONbits.TMR0IF = 0;
	/*
	TMR0IF: TMR0 Overflow Interrupt Flag bit
	1 = TMR0 register has overflowed (must be cleared in software)
	0 = TMR0 register did not overflow
	*/
	// Init Timer value
	TMR0H = _TMR0H_INITVALUE_;
	TMR0L = _TMR0L_INITVALUE_;	
	// Timer ON
	T0CONbits.TMR0ON = 1;	
}

/**************************************************************
 *  Name                 : timer0_finished
 *  Description          : Returns TRUE if the timer0 has finished
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
T_UBYTE timer0_finished(void)
{
	T_UBYTE lub_status;
    //check IF flag - timer expired
	if (INTCONbits.TMR0IF)
	{
        // start timer again
		TMR0H = _TMR0H_INITVALUE_;
		TMR0L = _TMR0L_INITVALUE_;		
        // clear IF flag
		INTCONbits.TMR0IF = 0;
        // set status to TRUE
		lub_status = TRUE;
	}
	else 
	{
		lub_status = FALSE;
	}
    return lub_status;
}

/**************************************************************
 *  Name                 : timer0_Interrupt_Handler
 *  Description          : This is the handler for timer 0 interrupt
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
 void timer0_Interrupt_Handler(void)
 {
	// Increment TimerCounter
	if ( ruw_TimerCounter == 0xFFFFFFFF )
	{
	    ruw_TimerCounter = 1;
	}
	else
	{
	    ruw_TimerCounter++;
	}
	// Reload timer 0
	Set_Timer0m16b(_TMR0_INITVALUE_);
	// or it can be used as
	//Set_Timer0_16b(_TMR0H_INITVALUE_,_TMR0L_INITVALUE_);
 }